package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.ShoppingServiceApplication;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author LC_RSH
 * @date 2022/7/8 21:09
 */
@RestController
@RequestMapping("shopping")
public class ShoppingController {
    @Autowired
    IHomeService iHomeService;

    @Autowired
    IContentService iContentService;

    @Autowired
    IProductCateService iProductCateService;

    @Autowired
    IProductService iProductService;

    /**
     * 商品主页显示
     *
     * @return position为0的panel及content信息
     * @author LC_R-SH
     * @since 2022/07/09 8:53
     */
    @GetMapping("homepage")
    public ResponseData homepage() {
        // 调用service方法
        HomePageResponse response = iHomeService.homepage();
        // 判断service方法返回值是否成功,成功则返回数据
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 不成功返回失败信息
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 导航栏显示
     *
     * @return panelId为0的content信息
     * @author LC_R-SH
     * @since 2022/07/09 9:07
     */
    @GetMapping("navigation")
    public ResponseData navigation() {
        // 调用service方法
        NavListResponse response = iContentService.queryNavList();
        // 判断
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        // 不成功
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("categories")
    // 非必须接收 无时默认为1
    public ResponseData categories(@RequestParam(required = false, defaultValue = "1") String sort) {
        AllProductCateRequest request = new AllProductCateRequest();
        if ("1".equals(sort)) {
            // 默认升序
            request.setSort("asc");
        } else {
            request.setSort("desc");
        }
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("recommend")
    public ResponseData recommend() {
        // 调用service方法
        RecommendResponse response = iProductService.getRecommendGoods();
        // 判断
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        // 不成功
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}

/**
 * @author 86157
 * @since 2022/07/08 21:09
 */